package com.gqh.weather.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gqh.weather.auth.UserContext;
import com.gqh.weather.auth.UserInfo;
import com.gqh.weather.domain.dto.AccountCreateRequest;
import com.gqh.weather.domain.dto.LoginRequest;
import com.gqh.weather.domain.dto.LoginResponse;
import com.gqh.weather.domain.dto.MyPage;
import com.gqh.weather.domain.dto.PageRequest;
import com.gqh.weather.domain.entity.Account;
import com.gqh.weather.exception.Result;
import com.gqh.weather.service.AccountService;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

/**
 * 账号管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/pingjia/zh/v1/account")
@Validated
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 账号密码登录
     *
     * @param request 登录请求
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(@Valid LoginRequest request) {
        log.info("用户登录，账号: {}", request.getAccount());
        return accountService.login(request);
    }

    /**
     * 获取账号详情
     *
     * @return 账号详情
     */
    @GetMapping("/info")
    public Result<Account> getAccountInfo() {
        Integer currentUserId = UserContext.getCurrentUserId();
        log.info("获取账号详情，ID {}", currentUserId);
        return accountService.getAccountInfo(currentUserId);
    }

    /**
     * 创建新账号
     *
     * @param request 创建请求
     * @return 创建结果
     */
    @PostMapping("/create")
    public Result<Void> createAccount(@Valid AccountCreateRequest request) {
        log.info("创建账号，账号: {}", request.getUsername());
        return accountService.createAccount(request);
    }

    /**
     * 更新账号信息
     *
     * @param account 账号对象
     * @return 更新结果
     */
    @PostMapping("/update")
    public Result<Void> updateAccount(@Valid Account account) {
        log.info("更新账号，ID: {}", account.getId());
        return accountService.updateAccount(account);
    }

    /**
     * 修改个人资料
     *
     * @param account 账号对象
     * @return 修改结果
     */
    @PostMapping("/modify")
    public Result<Void> modifyAccount(@Valid Account account) {
        log.info("修改个人资料，ID: {}", account.getId());
        // 限制只能修改部分字段
        UserInfo userInfo = UserContext.getUserInfo();

        Account updateAccount = new Account();
        updateAccount.setId(userInfo.getUid());
        updateAccount.setUsername(account.getUsername());
        updateAccount.setMobile(account.getMobile());

        return accountService.updateAccount(updateAccount);
    }

    /**
     * 修改密码
     *
     * @param id          账号ID
     * @param password 旧密码
     * @param newpwd 新密码
     * @return 修改结果
     */
    @PostMapping("/changepwd")
    public Result<Void> changePassword(
            @RequestParam @NotNull String password,
            @RequestParam @NotNull String newpwd) {
        log.info("修改密码，账号ID: {}", UserContext.getCurrentUserId());
        return accountService.changePassword(UserContext.getCurrentUserId(), password, newpwd);
    }

    /**
     * 删除账号
     *
     * @param id 账号ID
     * @return 删除结果
     */
    @PostMapping("/delete")
    public Result<Void> deleteAccount(@RequestParam @NotNull Integer id) {
        log.info("删除账号，ID: {}", id);
        return accountService.deleteAccount(id);
    }

    /**
     * 锁定/解锁账号
     *
     * @param id   账号ID
     * @param flag 锁定标志：0启用 1禁用
     * @return 操作结果
     */
    @PostMapping("/lock")
    public Result<Void> lockAccount(@RequestParam @NotNull Integer id,
            @RequestParam @NotNull Integer flag) {
        log.info("锁定/解锁账号，ID: {}, flag: {}", id, flag);
        return accountService.lockAccount(id, flag);
    }

    /**
     * 账号列表查询
     *
     * @param pageRequest 分页参数
     * @param citycode    城市码（权限过滤）
     * @return 查询结果
     */
    @GetMapping("/list")
    public Result<MyPage<Account>> getAccountList(@Valid PageRequest<Account> pageRequest,
            @RequestParam(required = false) String citycode) {
        log.info("查询账号列表，页码: {}, 数量: {}, 关键字: {}, 城市码: {}", 
                pageRequest.getPage(), pageRequest.getCount(), pageRequest.getKeyword(), citycode);
        return accountService.getAccountList(pageRequest, citycode);
    }
}